package com.glory.cqaospda.view

import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import com.glory.cqaospda.R
import com.glory.cqaospda.base.Constant
import com.glory.mvvmnet.base.BaseActivity
import com.glory.mvvmnet.base.BaseViewModel
import com.glory.mvvmnet.entity.ToastMessageBean
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ActivityManager
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils
import kotlinx.android.synthetic.main.activity_system_set.*

class SystemSetActivity : BaseActivity<BaseViewModel>(), View.OnClickListener {

    override fun layoutId(): Int {
        return R.layout.activity_system_set
    }

    override fun initView(savedInstanceState: Bundle?) {

        FreeApi_ActivityManager.getActivityManager().addActivity(this)

        tvServerText.setText(Constant.mUrl)
        rlServerAddress.setOnClickListener(this)

        rlFileAddress.setOnClickListener(this)
        ivBack.setOnClickListener(this)
        rlConnectTimeout.setOnClickListener(this)
        btY.setOnClickListener(this)
        btN.setOnClickListener(this)

        if (Constant.isUpdate) {
            btY.isChecked = true
            btN.isChecked = false
        } else {
            btN.isChecked = true
            btY.isChecked = false
        }

        val manager = packageManager
        var info: PackageInfo? = null
        try {
            info = manager.getPackageInfo(packageName, 0)
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        val version = info!!.versionName
        tvVersion.text = version
    }

    override fun initData() {

        val spConnectTimeout = FreeApi_SharePreferencesUtils.getSharePre(
            "SystemSet",
            "connectTimeout",
            applicationContext
        )
        val fileUrl = FreeApi_SharePreferencesUtils.getSharePre(
            "SystemSet",
            "fileUrl",
            applicationContext
        )
        tvFileText.setText(fileUrl)
        var i: Int
        if (spConnectTimeout.equals("")) {
            i = Constant.connectTimeout
        } else {
            i = spConnectTimeout.toInt()
        }
//        tvFileText.text = Constant.REQUEST_URL_DEBUG
        connectTimeout.text = i.toString() + "秒"
    }

    @SuppressLint("SetTextI18n")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (resultCode) {
            1 -> {
                val extras = data!!.getExtras()
                val string = extras!!.getString("server_address")
                tvServerText.setText(string)
                FreeApi_SharePreferencesUtils.setSharePre(
                    "SystemSet",
                    "sercerUrl",
                    string,
                    applicationContext
                )
                Constant.mUrl = string!!
//                Constant.mainUrl = Constant.http + Constant.mUrl + Constant.url
                viewModel.defUI.toastEvent.value = ToastMessageBean("保存成功", true)
            }
            2 -> {
                val extras = data!!.getExtras()
                val string = extras!!.getString("file_address")
                tvFileText.setText(string)
                FreeApi_SharePreferencesUtils.setSharePre(
                    "SystemSet",
                    "fileUrl",
                    string,
                    applicationContext
                )
                Constant.fileUrl = string!!
                viewModel.defUI.toastEvent.value = ToastMessageBean("保存成功", true)
            }
            4 -> {
                val extras = data!!.getExtras()
                val string = extras!!.getString("connectTimeout")
                if (string.equals("")) {
                    connectTimeout.setText("")
                } else {
                    connectTimeout.setText(string + "秒")
                }

                FreeApi_SharePreferencesUtils.setSharePre(
                    "SystemSet",
                    "connectTimeout",
                    string,
                    applicationContext
                )

                Constant.connectTimeout = string!!.toInt()
                viewModel.defUI.toastEvent.value = ToastMessageBean("保存成功", true)

            }
            108 -> {
                finish()
            }
        }
    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            intent.putExtra("type", "KeyEvent")
            intent.setClass(this, SkipDialogActivity::class.java)
            startActivityForResult(intent, 1)
            return false
        }
        return super.onKeyUp(keyCode, event)
    }

    override fun onClick(view: View?) {
        when (view) {
            rlServerAddress -> {
                val intent = Intent()
                intent.putExtra("type", "server_address")
                intent.setClass(this, SkipDialogActivity::class.java)
                startActivityForResult(intent, 1)
            }
            rlFileAddress -> {
                val intent = Intent()
                intent.putExtra("type", "file_address")
                intent.setClass(this, SkipDialogActivity::class.java)
                startActivityForResult(intent, 1)
            }
            ivBack -> {
                finish()
            }
            rlConnectTimeout -> {
                val intent = Intent()
                intent.putExtra("type", "connectTimeout")
                intent.setClass(this, SkipDialogActivity::class.java)
                startActivityForResult(intent, 1)
            }
            btY -> {
                FreeApi_SharePreferencesUtils.setSharePre(
                    "SystemSet",
                    "isUpdate",
                    "Y",
                    applicationContext
                )
                Constant.isUpdate = true
                viewModel.defUI.toastEvent.value = ToastMessageBean("已开启自动更新", true)
            }
            btN -> {
                FreeApi_SharePreferencesUtils.setSharePre(
                    "SystemSet",
                    "isUpdate",
                    "N",
                    applicationContext
                )
                Constant.isUpdate = false
                viewModel.defUI.toastEvent.value = ToastMessageBean("已关闭自动更新", true)
            }
        }
    }

}